package com.saville.msgcomp;

public class McConst {
    /*
     * The lower 4 bits of a command are reserved.
     * 
     * BIT0, REQUIRES reply
     * BIT1, is a reply
     */
    public static final int CMD_NEEDS_REPLY = 0x01;
    public static final int CMD_REPLY = 0x02;
    public static final int CMD_REQ_REPLY_MASK = CMD_NEEDS_REPLY | CMD_REPLY;
    public static final int CMD_SHIFT = 4;
    public static final int MC_GUID = -1;
    
    public static final int mkCmd(int v) { return (v << CMD_SHIFT); }
    public static final int mkRequest(int v) { return mkCmd(v) | CMD_NEEDS_REPLY; }
    public static final int mkReply(int v) { return mkCmd(v) | CMD_REPLY; }
    
    public static final int getCmd(int cmd) { return (cmd>> CMD_SHIFT); }
    public static final int cmdToRequest(int cmd) { return mkRequest(getCmd(cmd)); }
    public static final int cmdToReply(int cmd) { return mkReply(getCmd(cmd)); }
    public static final boolean cmdIsRequest(int cmd) { return ((cmd & CMD_REQ_REPLY_MASK) == CMD_NEEDS_REPLY); }
    public static final boolean cmdIsReply(int cmd) { return ((cmd & CMD_REQ_REPLY_MASK) == CMD_REPLY); }

    /*
     * Well know ports
     */
    public static final int MCMGR_PRIMARY_PORT = 47000;
    public static final int MCMASTER_PORT = 46000;

    /*
     * Well known commands
     */
    public static final int MC_CMD_NOP = mkCmd(0);
    public static final int MC_CMD_STARTED = mkCmd(1);
    public static final int MC_CMD_ECHO = mkRequest(2);
    
    public static final int MCMGR_CMD_GET_SECONDARY_PORT = mkRequest(3);
    public static final int MCMGR_CMD_REGISTER_MC = mkRequest(4);
    public static final int MCMGR_CMD_LOOKUP_MC = mkRequest(5);
    public static final int MCMGR_CMD_GET_SESSION_ID_SET = mkRequest(6);
    
    
    public static final int MCMASTER_GUID = -2;
    public static final int MCMASTER_CMD_REGISTER_DEV = mkRequest(1);
    public static final int MCMASTER_CMD_LOOKUP_DEV = mkRequest(2);
    public static final int MCMASTER_CMD_FORWARD_MSG = mkCmd(3);
    
    public static final int MCPIPE_GUID = -3;
    public static final int MCPIPE_CMD_CONNECTED = mkCmd(1);
    public static final int MCPIPE_CMD_CLOSED = mkCmd(2);
    public static final int MCPIPE_CMD_PRODUCER_MSG = mkCmd(3);
    public static final int MCPIPE_CMD_PRODUCER_HTTP_HEADER_MSG = mkCmd(4);
    public static final int MCPIPE_CMD_PRODUCER_HTTP_GET_MSG = mkCmd(5);
    public static final int MCPIPE_CMD_NON_AMP_CMD = mkCmd(6);
    
    /*
     * Well known status values
     */
    public static final int STATUS_OK = 0;
    public static final int STATUS_ERR = 1;
    public static final int STATUS_UNKNOWN_CMD = 2;
    public static final int STATUS_ECHO = 3;
    
    /*
     * A bit used for some debugging, TestHmcServer.java
     * it modifies the command to be "noisy" and output additional debug
     */
    public static final int NOISY = 0x80000;
}
